#!/bin/bash

echo "🚀 MongoDB Sync Setup and Test"
echo "==============================="

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 步骤1: 停止现有容器
print_info "Step 1: Stopping existing containers..."
docker-compose -f docker-compose-fixed.yml down -v

# 步骤2: 启动数据库
print_info "Step 2: Starting MongoDB databases..."
docker-compose -f docker-compose-fixed.yml up -d mongodb-source mongodb-target

# 步骤3: 等待数据库初始化
print_info "Step 3: Waiting for database initialization..."
sleep 45

# 步骤4: 检查数据库状态
print_info "Step 4: Checking database status..."
if docker-compose -f docker-compose-fixed.yml ps | grep -q "Up"; then
    print_success "Databases are running"
else
    print_error "Databases failed to start"
    exit 1
fi

# 步骤5: 运行诊断
print_info "Step 5: Running diagnosis..."
chmod +x diagnose-sync.sh
./diagnose-sync.sh

# 步骤6: 执行同步
print_info "Step 6: Starting sync process..."
chmod +x sync-scripts/fixed-sync.sh
docker-compose -f docker-compose-fixed.yml --profile sync run --rm mongosync /home/mongosync/scripts/fixed-sync.sh

# 步骤7: 验证结果
print_info "Step 7: Final verification..."
echo ""
echo "📊 Final Status:"
echo "================"
echo "Source Database (Port 20727):"
echo "  Connection: mongodb://admin:password1@localhost:20727/admin"
echo "  Users: $(docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').users.countDocuments()")"
echo "  Products: $(docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').products.countDocuments()")"
echo "  Orders: $(docker-compose -f docker-compose-fixed.yml exec mongodb-source mongosh --username admin --password password1 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').orders.countDocuments()")"

echo ""
echo "Target Database (Port 20728):"
echo "  Connection: mongodb://admin:password2@localhost:20728/admin"
echo "  Users: $(docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').users.countDocuments()")"
echo "  Products: $(docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').products.countDocuments()")"
echo "  Orders: $(docker-compose -f docker-compose-fixed.yml exec mongodb-target mongosh --username admin --password password2 --authenticationDatabase admin --quiet --eval "db.getSiblingDB('testdb').orders.countDocuments()")"

echo ""
print_success "Setup and sync completed!"
echo ""
print_info "Useful commands:"
echo "  View logs: docker-compose -f docker-compose-fixed.yml logs"
echo "  Stop all: docker-compose -f docker-compose-fixed.yml down"
echo "  Connect to source: mongosh mongodb://admin:password1@localhost:20727/admin"
echo "  Connect to target: mongosh mongodb://admin:password2@localhost:20728/admin" 